package com.ruoyi.common.utils.phone;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtility {

	private static final Pattern TAG_PATTERN = Pattern.compile("#([^\\#|.]+)#");

	private static final Pattern MOBILE_PATTERN = Pattern
			.compile("^1[3-8]\\d{9}$");

	private static final Pattern EMAIL_PATTERN = Pattern
			.compile("^[A-Za-z0-9-_.]+@[A-Za-z0-9-_.]+.[A-Za-z]{2,5}$");

	private static final Pattern AT_PATTERN = Pattern
			.compile("@[\\u4e00-\\u9fa5\\w\\-]+");

	/**
	 * 验证邮箱地址是否正确
	 * 
	 * @param email
	 * @return
	 */
	public static boolean checkEmail(String email) {
		try {
			Matcher matcher = EMAIL_PATTERN.matcher(email);
			return matcher.matches();
		} catch (Exception e) {

		}
		return false;
	}

	/**
	 * 验证手机号码是否正确
	 * 
	 * @param
	 * @return
	 */
	public static boolean checkMobilePhone(String mobile) {
		try {
			Matcher matcher = MOBILE_PATTERN.matcher(mobile);
			return matcher.matches();
		} catch (Exception e) {

		}
		return false;
	}

	/**
	 * 提取话题
	 * */
	public static ArrayList<String> tagsFromString(String content) {
		if (content == null || content.length() == 0) {
			return null;
		}

		Matcher m = TAG_PATTERN.matcher(content);
		ArrayList<String> tags = new ArrayList<String>();
		while (m.find()) {
			String tag = m.group();
			String formated = tag.replace('#', ' ').trim();
			if (formated.length() > 0) {
				tags.add(formated);
			}
		}
		return tags;
	}

	/**
	 * 提取用户
	 * */
	public static ArrayList<String> usersFromString(String content) {
		if (content == null || content.length() == 0) {
			return null;
		}

		Matcher m = AT_PATTERN.matcher(content);
		ArrayList<String> users = new ArrayList<String>();
		while (m.find()) {
			String tag = m.group();
			String formated = tag.replace('@', ' ').trim();
			if (formated.length() > 0) {
				users.add(formated);
			}
		}
		return users;
	}

	public RegexUtility() {

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(tagsFromString("#111# # 1#11# #"));
	}

}
